package com.intelligent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.intelligent.pojo.Product;
import com.intelligent.service.ProductService;
import com.intelligent.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品控制器
 */
@Api(tags = "产品管理")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation("获取产品列表")
    @GetMapping("/list")
    public Result<List<Product>> list(@RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize) {
        try {
            LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
            wrapper.last("LIMIT " + pageSize);
            List<Product> products = productService.list(wrapper);
            return Result.success(products);
        } catch (Exception e) {
            return Result.error("获取产品列表失败: " + e.getMessage());
        }
    }

    @ApiOperation("根据ID获取产品")
    @GetMapping("/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        try {
            Product product = productService.getById(id);
            if (product == null) {
                return Result.error("产品不存在");
            }
            return Result.success(product);
        } catch (Exception e) {
            return Result.error("获取产品失败: " + e.getMessage());
        }
    }
}
